<template>
	<view class="contianer" :class="'wp-' + platform">
		<cu-custom :bgColor="theme.bgnav" :isBack="true" Barborder="solid-bottom">
			<block slot="content">会议详情</block>
		</cu-custom>
		<view class="mainpageitem">
			<view class="metting-box">
				<view class="user-box" v-if="callInfo.sender">
					<!--  shape="square" -->
					<u-avatar :size="60" :src="getAvatar(callInfo.sender)" @click="handlerAvatar(callInfo.sender)">
					</u-avatar>
					<text class="user-name">{{getMettingUser(callInfo)}}</text>
				</view>
				
				<view class="cu-list menu" @click="handleCopy()">
					<view class="cu-item" style="padding: 20upx 30upx;">
						<view class="content">
							<text>会议号</text>
						</view>
						<view class="action" style="max-width: 70%;">
							{{callInfo.extra}}
						</view>
					</view>
					
					<view v-if="callInfo.title" class="cu-item" style="padding: 20upx 30upx;">
						<view class="content">
							<text>会议主题</text>
						</view>
						<view class="action" style="max-width: 70%;">
							{{callInfo.title}}
						</view>
					</view>
					<view v-if="callInfo.description" class="cu-item" style="padding: 20upx 30upx;">
						<view class="content">
							<text>会议内容</text>
						</view>
						<view class="action" style="max-width: 70%;">
							{{callInfo.description}}
						</view>
					</view>
					<view v-if="callInfo.enterTime" class="cu-item" style="padding: 20upx 30upx;">
						<view class="content">
							<text>入会时间</text>
						</view>
						<view class="action" style="max-width: 70%;">
							{{callInfo.enterTime}}
						</view>
					</view>
					<view v-if="callInfo.enterTime" class="cu-item" style="padding: 20upx 30upx;">
						<view class="content">
							<text>参会时长</text>
						</view>
						<view class="action" style="max-width: 70%;">
							{{callInfo.callTime || '00:00:00'}}
						</view>
					</view>
					<!-- <view v-if="callInfo.enterTime" class="cu-item" style="padding: 20upx 30upx;">
						<view class="content">
							<text>参会时长</text>
						</view>
						<view class="action" style="max-width: 70%;">
							{{callInfo.callTime || '00:00:00'}}
						</view>
					</view> -->
					
					<view class="cu-item" style="padding: 20upx 30upx;">
						<view class="content">
							<text>会议类型</text>
						</view>
						<view class="action" style="max-width: 70%;">
							{{callInfo.type=='quick'?'快速会议':'预约会议'}}
						</view>
					</view>
					
				</view>
				
				<view class="user-box-list">
					
					<view class="user-item" v-for="(name, uid) in userList">
						<u-avatar :size="30" :src="getAvatar(uid)" @click="handlerAvatar(uid)">
						</u-avatar>
						<text class="user-name">{{name}}</text>
					</view>
					
				</view>
			</view>
			
			
			<view class="flex flex-direction margin-top padding">
				<button class="cu-btn lg bg-limegreen" @tap="handleAddMetting">立即加入</button>
			</view>
			<view class="flex flex-direction padding">
				<button class="cu-btn lg bg-limegreen" @tap="handleForwardMessage">转发</button>
			</view>
		</view>
	</view>
</template>

<script>
import {
  findCall,
  findUser,
  updateMessageId,
  updateReply,
  insertMessage,
  assembleMessage,
  isReply
} from '@/data/common'
import * as API_liveKit from '@/api/livekit'
import * as API_message from '@/api/message';
import store from '@/store';
import core from '@/core';
export default {
	data(){
		return{
			theme: core.style,
			text: core.langs,
			platform: core.platform,
			callInfo:{},
			forwardMsg:{},
			userList:[],
		}
	},
	onLoad(options){
		this.getInfo(options.id)
	},
	onReady() {
		uni.$off('Forward');
		uni.$on('Forward', (selectList, type) => {
			//转发
			this.forwardMessage(selectList, type)
		});
	},
	methods:{
		handleForwardMessage(){
			this.forwardMsg = this.callInfo
			core.navito('dialog/repyList');
		},
		// 
		async forwardMessage(selectList, type){ //转发
		  return new Promise(async (resolve, reject) => {
			let len = selectList.length
			let num = 0
			let action = 0
			let id = null
			let receivers = []
			let forwardMsg = { ...this.forwardMsg }
			let forwardMsgs = []
			while (num < len) {
			  const item = selectList[num]
			  let params = {
				content: forwardMsg.content,
				format: forwardMsg.format,
			  }
			  forwardMsg.fail = 0
			  if (type == 'chat') { //转发到个人
				console.log('转发到单人')
				forwardMsg.receiver = item.id
				forwardMsg.sender = store.getters.user.id
				forwardMsg.action = '0'
				params.uid = item.id
				forwardMsg.type = 'chat'
				id = item.id
				action = '0'
				delete forwardMsg.title
				forwardMsg.age = 0
			  } else if (type == 'group') { //转发到群
				console.log('转发到群')
				forwardMsg.sender = item.id
				forwardMsg.extra = store.getters.user.id
				forwardMsg.receiver = store.getters.user.id
				params.groupId = item.id
				forwardMsg.type = 'group'
				item.title = JSON.stringify({
				  name: store.getters.user.name
				})
				forwardMsg.title = JSON.stringify({
				  name: store.getters.user.name
				})
				action = '3'
				forwardMsg.action = '3'
				id = item.id
				forwardMsg.age = 0
			  } else if (type == 'reply') { // 转发到回话
				console.log('转发到会话')
				forwardMsg.fail = item.remove
				if (item.type == 'chat') { // 消息的类型个人
				  params.uid = item.sender
				  forwardMsg.receiver = item.sender
				  forwardMsg.sender = store.getters.user.id
				  type = 'chat'
				  forwardMsg.type = 'chat'
				  action = '0'
				  id = item.sender
				  forwardMsg.action = '0'
				  delete forwardMsg.title
				  if (item.age && siteinfo.burn_down) forwardMsg.age = item.age
				  else forwardMsg.age = 0
				}
				if (item.type == 'group') { // 消息的类型群
				  params.groupId = item.sender
				  id = item.sender
				  forwardMsg.receiver = item.receiver
				  forwardMsg.sender = item.sender
				  item.title = JSON.stringify({
					name: store.getters.user.name
				  })
				  type = 'group'
				  forwardMsg.type = 'group'
				  forwardMsg.extra = store.getters.user.id
				  forwardMsg.action = '3'
				  forwardMsg.title = JSON.stringify({
					name: store.getters.user.name
				  })
				  action = '3'
				  if (item.age && siteinfo.burn_down) forwardMsg.age = item.age
				  else forwardMsg.age = 0
				}
			  }
			  let receiver = {
				action: forwardMsg.action,
				id: action == '3' ? forwardMsg.sender : forwardMsg.receiver,
				age: 0
			  }
			  receivers.push(receiver)
			  forwardMsg.timestamp = new Date().getTime() // 本地记录发送时间
			  forwardMsg.createTime = new Date().getTime() // 本地记录发送时间
			  forwardMsg.id = new Date().getTime()
			  let appendixId = -1
			  forwardMsg.appendixId = appendixId
			  delete forwardMsg.messageId
			  if (item.remove === 1) return // 会话或者群或者人被删除了已经
			  forwardMsg.unread = 1
			  forwardMsg.content = `邀请您加入会议\n房间号码：${this.forwardMsg.extra}`
			  const handleMsg = await assembleMessage(forwardMsg, 1)
			  forwardMsgs.push(handleMsg)
			  num++
			}
			let params = {
			  content: `邀请您加入会议\n房间号码：${this.forwardMsg.extra}`,
			  format: this.forwardMsg.format,
			  receiver: receivers,
			};
			if (this.forwardMsg.format == '6') {
			  params.format = '6'
			  params.content = JSON.stringify({
				title: JSON.parse(this.forwardMsg.content).title,
				url: JSON.parse(this.forwardMsg.content).url
			  })
			}
			API_message.messageForward(params).then(async (resp) => {
			  if (resp.code == 200) {
				for (let i = 0; i < forwardMsgs.length; i++) {
				  const item = forwardMsgs[i];
				  for (let j = 0; j < resp.data.length; j++) {
					if ((resp.data[j].id == item.sender && item.action == '3' && '3' == resp.data[j].action) || (
					  resp.data[j].id == item.receiver && item.action == '0' && '0' == resp.data[j].action)) {
					  item.messageId = resp.data[j].mid
					  item.loading = 0
					  const lastId = await insertMessage(item) // 插入数据
					  await updateMessageId(resp.data[j].mid, lastId, resp.data[j].timestamp)
					  if (item.action == '3') {
						console.log('更新群数据', item)
						await isReply({
						  ...item,
						  id: resp.data[j].mid,
						  timestamp: resp.data[j].timestamp,
						  createTime: resp.data[j].timestamp,
						}, item.sender, item.receiver)
						await updateReply()
					  } else {
						console.log('单人数据', item)
						await isReply({
						  ...item,
						  id: resp.data[j].mid,
						  timestamp: resp.data[j].timestamp,
						  createTime: resp.data[j].timestamp,
						}, item.receiver, item.sender)
						await updateReply()
					  }
					}
				  }
				}
				resolve()
			  }
			});
		  })
		},
		async getInfo(id){
		  let res = await findCall(id);
		  console.log('res---->',res);
		  this.callInfo = res;
		  this.userList = JSON.parse(res.memberList)
		},
		// 
		async handleAddMetting(){
			if (this.$nativeRtc.isUIKitEnable()) {
				const roomResp = await this.handleGetRoomInfo(this.callInfo.extra);
				// console.log('开始加入',this.callInfo)
				// 调用原生插件
				this.$nativeRtc.onJoinMeeting({
					"uid":this.callInfo.sender,
					"name":this.getMettingUser(this.callInfo),
					"tag":this.callInfo.extra,
					"title":this.callInfo.title,
					"description":this.callInfo.description,
					"muted":false,
					"microphoneOn":false,
					"cameraOn":false,
					"fromGroupMode":false,
				})
				return;
			}
			this.naviTo('livekit/metting?type=metting&tag='+this.callInfo.extra,{},2);
		},
		getMettingUser(item){
			if(!item) return;
			const members = JSON.parse(item.memberList)
			return members[item.sender]
		},
		handlerAvatar(uid) {
			if(uid == this.user.id) return;
			this.naviTo('member/index', {
				uid: uid,
			})
		},
		// 获取房间详情
		async handleGetRoomInfo(roomTag){
			let params = {
				roomTag: roomTag
			}
			const resp = await API_liveKit.getRoomInfo(params)
			if (resp.code == 200) {
				return resp
			} else {
				this.toast('房间不存在')
				return false
			}
		
		},
		handleCopy(){
			let text = this.callInfo.extra;
			core.copy(text,'会议号已复制到粘贴板')
		}
	}
}
</script>

<style lang="scss" scoped>
.metting-box{
	width: 90%;
	margin: 40rpx auto;
	border-radius: 20rpx;
	background-color: #fff;
	padding: 20rpx;
	.user-box{
		display: flex;
		flex-direction: column;
		align-items: center;
		.user-name{
			margin-top: 20rpx;
		}
	}
	.user-box-list{
		display:flex;
		flex-wrap: wrap;
		.user-item{
			margin-left:20rpx;
			display:flex;
			flex-direction: column;
			align-items: center;
			.user-name{
				margin-top: 20rpx;
				font-size:24rpx;
			}
		}
	}
}
</style>